﻿
Namespace Commands

    <CLSCompliant(True), Serializable()>
    Public Class ActionCommand
        Implements ICommand
#Region "Fields"

        Private ReadOnly m_Action As Action(Of Object)
        Private ReadOnly m_CanExecute As Predicate(Of Object)

#End Region

#Region "Constructors"

        ''' <summary>
        ''' Creates a new ActionCommand that can always execute.
        ''' </summary>
        ''' <param name="action">The execution logic.</param>
        Public Sub New(action As Action(Of Object))
            If action Is Nothing Then
                Throw New ArgumentNullException("Action")
            End If
            m_Action = action
        End Sub

        ''' <summary>
        ''' Creates a new ActionCommand that can always execute.
        ''' </summary>
        ''' <param name="action">The execution logic.</param>
        ''' <param name="canExecute">The execution status logic.</param>
        Public Sub New(action As Action(Of Object), canExecute As Predicate(Of Object))
            If action Is Nothing Then
                Throw New ArgumentNullException("action")
            End If
            m_Action = action
            m_CanExecute = canExecute
        End Sub




#End Region

#Region "ICommand Members"

        <DebuggerStepThrough()> _
        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
            Return If(m_CanExecute Is Nothing, True, m_CanExecute(parameter))
        End Function

        Public Sub OnCanExecuteChanged(sender As Object, e As EventArgs)
            Try
                RaiseEvent CanExecuteChanged(sender, e)
            Catch ex As Exception
                ' threading error
            End Try

        End Sub

        Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged


        Public Sub Execute(parameter As Object) Implements ICommand.Execute
            m_Action(parameter)
        End Sub

#End Region

        'Public Property CommandCanExecute() As Boolean
        '    Get
        '        Return m_CommandCanExecute
        '    End Get
        '    Set(ByVal value As Boolean)
        '        If m_CommandCanExecute = value Then
        '            Return
        '        End If

        '        m_CommandCanExecute = value

        '    End Set
        'End Property
        'Private m_CommandCanExecute As Boolean


    End Class
End Namespace
